package Communication;

import java.net.*;
import java.io.*;

public class ZooServer implements Runnable{

    Thread runner;
    int portNumber;
    Socket s;
    ServerSocket ss;
    ObjectInputStream ois;
    ObjectOutputStream oos;

    public ZooServer(int portNumber) throws IOException, ClassNotFoundException {
        this.portNumber = portNumber;
        System.out.println("1111111111111");



        System.out.println("22222222222222222222");
        System.out.println("Starting!!...");

        System.out.println("Make Server Socket!!...");
        ss = new ServerSocket(portNumber);

        System.out.println("Bind Server Socket!!...");
        //ss.bind(new InetSocketAddress());



        runner = new Thread(this);
        System.out.println(runner.getName());
        runner.start();
    }


    public void listenForClient() throws IOException, ClassNotFoundException {
        try {
            System.out.println("Socket Made - Waiting to Accept!!...");
            s = ss.accept();
        } catch (Exception yui) {
            System.out.println(yui);
        }
    }


    public void sendUpdate() throws IOException, ClassNotFoundException {
        try {

            System.out.println("Server sending data to client!!!");

            oos = new ObjectOutputStream(s.getOutputStream());
            oos.writeObject("this UPDATE SENT from SERVER");
        } catch (Exception yui) {
            System.out.println(yui);
        }
    }


    public void receiveUpdate() throws IOException, ClassNotFoundException {
        try {

            System.out.println("Server is Looking for Received Data!!!");

            ois = new ObjectInputStream(s.getInputStream());
            String message = (String) ois.readObject();
            System.out.println("Message Received: " + message);
        } catch (Exception yui) {
            System.out.println(yui);
        }
    }


    public String getConnectionDetails() throws IOException, ClassNotFoundException {
        String str = "NULL CONNECTIONS";
        try {

          str = s.getInetAddress().toString();
        } catch (Exception yui) {
            System.out.println(yui);
        }
        return str;
    }


    public void run() {
        try {
            System.out.println("3333333333333333333333333");
            //listenForUpdate();

        } catch(Exception ert) {

        }

    }
}
